import WeDisplayShaders from './WeDisplayShaders';

class WeDisplayEffectNormal {
  constructor() {

  }

  pushDefines(defines) {
    defines.push(WeDisplayShaders.WeDefineHasNormalMap)
  }

  get shaderSource() {
    return `
        vec3 WeTexNormalMap(in vec2 uv)
        {
            float minValue = Weget_minValue();
            float maxValue = Weget_maxValue();
            float normalFactor = Weget_normalFactor();
            vec2 heightMapResolution = u_textureResolution;
            vec3 normal;
            vec3 height;
            vec2 offset = 1.0 / heightMapResolution.xy;
            height.x = WeDecodeValue(uv);
            height.y = WeDecodeValue(uv + vec2(offset.x, 0.0));
            height.z = WeDecodeValue(uv + vec2(0.0, offset.y));
            height = (height - minValue) / (maxValue - minValue);
            normal.xy = (height.x - height.yz);
            normal.xy /= offset;
            normal.z = normalFactor;
            normal = normalize(normal);
            normal = normal * 0.5 + 0.5;
            return normal;
        }

        void WeNormalEffect(inout vec4 color,in vec2 uv)
        {
            vec3 normal = WeTexNormalMap(uv);
            vec2 rot = vec2(1.0);
            vec3 ld = vec3(rot, 0.2); // light direction
            float ad = 0.1 + dot(ld, normal); // ambient + diffuse
            color.rgb = mix(color.rgb,vec3(0.1),1.0 - ad);
        }
        `;
  }
}

export default WeDisplayEffectNormal
